<!-- Any copyright is dedicated to the Public Domain.
   - http://creativecommons.org/publicdomain/zero/1.0/ -->
<!DOCTYPE HTML>
<html>
<head>
  <title>Validate Interfaces Exposed to Service Workers</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  <script type="text/javascript" src="../worker_driver.js"></script>
</head>
<body>
<script class="testbody" type="text/javascript">

  function setupSW(registration) {
    var worker = registration.waiting ||
                 registration.active;
    window.onmessage = function(event) {
      if (event.data.type == 'finish') {
        registration.unregister().then(function(success) {
          ok(success, "The service worker should be unregistered successfully");

          SimpleTest.finish();
        }, function(e) {
          dump("Unregistering the SW failed with " + e + "\n");
          SimpleTest.finish();
        });
      } else if (event.data.type == 'status') {
        ok(event.data.status, event.data.msg);

      } else if (event.data.type == 'getPrefs') {
        var result = {};
        event.data.prefs.forEach(function(pref) {
          result[pref] = SpecialPowers.Services.prefs.getBoolPref(pref);
        });
        worker.postMessage({
          type: 'returnPrefs',
          prefs: event.data.prefs,
          result: result
        });

      } else if (event.data.type == 'getVersion') {
        var result = SpecialPowers.Cc['@mozilla.org/xre/app-info;1'].getService(SpecialPowers.Ci.nsIXULAppInfo).version;
        worker.postMessage({
          type: 'returnVersion',
          result: result
        });

      } else if (event.data.type == 'getUserAgent') {
        worker.postMessage({
          type: 'returnUserAgent',
          result: navigator.userAgent
        });
      } else if (event.data.type == 'getOSCPU') {
        worker.postMessage({
          type: 'returnOSCPU',
          result: navigator.oscpu
        });
      }
    }

    worker.onerror = function(event) {
      ok(false, 'Worker had an error: ' + event.data);
      SimpleTest.finish();
    };

    var iframe = document.createElement("iframe");
    iframe.src = "message_receiver.html";
    iframe.onload = function() {
      worker.postMessage({ script: "test_serviceworker_interfaces.js" });
    };
    document.body.appendChild(iframe);
  }

  function runTest() {
    navigator.serviceWorker.ready.then(setupSW);
    navigator.serviceWorker.register("serviceworker_wrapper.js", {scope: "."});
  }

  SimpleTest.waitForExplicitFinish();
  onload = function() {
    // The handling of "dom.caches.enabled" here is a bit complicated.  What we
    // want to happen is that Cache is always enabled in service workers.  So
    // if service workers are disabled by default we want to force on both
    // service workers and "dom.caches.enabled".  But if service workers are
    // enabled by default, we do not want to mess with the "dom.caches.enabled"
    // value, since that would defeat the purpose of the test.  Use a subframe
    // to decide whether service workers are enabled by default, so we don't
    // force creation of our own Navigator object before our prefs are set.
    var prefs = [
      ["dom.serviceWorkers.exemptFromPerDomainMax", true],
      ["dom.serviceWorkers.enabled", true],
      ["dom.serviceWorkers.testing.enabled", true],
    ];

    var subframe = document.createElement("iframe");
    document.body.appendChild(subframe);
    if (!("serviceWorker" in subframe.contentWindow.navigator)) {
	prefs.push(["dom.caches.enabled", true]);
    }
    subframe.remove();

    SpecialPowers.pushPrefEnv({"set": prefs}, runTest);
  };
</script>
</body>
</html>
